package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.excpetion.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 敏感词过滤服务层
 */
@Service("AdSensitiveSrvice")
@Transactional(readOnly = true)
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseResult deleteSensitive(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        //TODO 前端传删除的敏感词

        AdSensitive key = super.getById(id);
        boolean result = super.removeById(id);
        if (result) {
            //从redis删除对应数据
            redisTemplate.opsForSet().remove(WemediaConstants.SENSITIVE, key.getSensitives());
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    /**
     * 分页查询敏感词
     *
     * @param sensitiveDTO 包含 名字 当前页 页面大小
     * @return
     */
    @Override
    public ResponseResult querySensitive(SensitiveDTO sensitiveDTO) {
        if (sensitiveDTO == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        //检查页面参数并初始化
        sensitiveDTO.checkParam();

        LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sensitiveDTO.getName()))
            lambdaQuery.like(AdSensitive::getSensitives, sensitiveDTO.getName()).orderByDesc(AdSensitive::getCreatedTime);

        Page<AdSensitive> page = new Page<>(sensitiveDTO.getPage(), sensitiveDTO.getSize());

        IPage<AdSensitive> sensitiveIPage = super.page(page, lambdaQuery);

        return new PageResponseResult(
                sensitiveDTO.getPage(),
                sensitiveDTO.getSize(),
                sensitiveIPage.getTotal(),
                sensitiveIPage.getRecords()
        );
    }

    /**
     * 保存敏感词信息
     *
     * @param adSensitive
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        if (adSensitive == null || adSensitive.getSensitives() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        adSensitive.setCreatedTime(new Date());
        boolean saveResult = super.save(adSensitive);
        //保存失败
        if (!saveResult) {
            throw new CustomException(AppHttpCodeEnum.SERVER_ERROR, "保存失败");
        }

        redisTemplate.opsForSet().add(WemediaConstants.SENSITIVE, adSensitive.getSensitives());

        return ResponseResult.okResult();

    }

    /**
     * 修改敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        if (adSensitive == null || adSensitive.getSensitives() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        adSensitive.setCreatedTime(new Date());

        redisTemplate.opsForSet().remove(WemediaConstants.SENSITIVE, adSensitive.getSensitives());
        boolean updateResult = super.updateById(adSensitive);

        //修改失败
        if (!updateResult) {
            throw new CustomException(AppHttpCodeEnum.SERVER_ERROR);
        }
        redisTemplate.opsForSet().add(WemediaConstants.SENSITIVE, adSensitive.getSensitives());
        return ResponseResult.okResult();
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    /**
     * 查询敏感词列表
     *
     * @return
     */
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
